package com.xy.algorithm;

public class Search {

    public static int binarySearch(int[] li, int val) {
        int left = 0;
        int right = li.length - 1;
        int mid = 0;
        while (left < right) {
            mid = (left + right) / 2;
            if (li[mid] == val) {
                return mid;
            } else if (li[mid] < val) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] li = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int val = 10;
        int index = binarySearch(li, val);
        System.out.println(index);
    }
}
